#pragma  once

#include"Log.hpp"
#include<string>
#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>


class Util
{
public:
 static int ReadLine(int sockfd,std::string &out)
 {
   if(sockfd<0)
   {
     LOG(WARNING,"sockfd is not exist!");
     return -1;
   }

   char c;
   while(recv(sockfd,&c,1,0)>0)
   {
    if(c!='\r'&&c!='\n')
    {
      out+=c;
    }

    else if(c=='\r') 
    {
      int ret=recv(sockfd,&c,1,MSG_PEEK);
      if(ret > 0)
      {
        if(c=='\n')
        {
          recv(sockfd,&c,1,0);
          out+="\n";
          return out.size();
        }
        else 
        {
          out+="\n";
          return out.size();
        }
      }
      else if(ret == 0) 
      {
        out+="\n";
        return out.size();
      }
      else 
      {
       LOG(ERROR,"Recv Error!");   
       return -3;
      }
    }
    else if(c=='\n')
    {
       out+=c;
       return out.size();
    }
    else 
    {
      LOG(ERROR,"Logical Error!");
      return -2;
    }
      

   }
    
   LOG(ERROR,"Recv Error!");
   return -3;

 }

 static int CutString(const std::string &target,std::string &out_1,std::string &out_2,const std::string &sep)
 {
   size_t pos=target.find(sep.c_str(),0);
   if(pos!=std::string::npos)
   {//找到了
     out_1=target.substr(0,pos); 
     out_2=target.substr(pos+sep.size());
     return 0;
   }

   //没找到
   out_1=target;
   return -1;
 }

};
